#include "stdafx.h"
#include "msg.h"

CMsgImpl::CMsgImpl(void)
{
    Clear();
}
CMsgImpl::~CMsgImpl(void)
{
    Clear();
}
HRESULT CMsgImpl::SetMsgType(unsigned int type)
{
    m_msgtype = type;
    return S_OK;
}
unsigned int CMsgImpl::GetMsgType()
{
    return m_msgtype;
}
HRESULT CMsgImpl::SetId(unsigned int id)
{
    m_id = id;
    return S_OK;
}
unsigned int CMsgImpl::GetId()
{
    return m_id;
}
HRESULT CMsgImpl::SetVersion(unsigned int version)
{
    m_version = version;
    return S_OK;
}
unsigned int CMsgImpl::GetVersion()
{
    return m_version;
}
HRESULT CMsgImpl::SetLength(unsigned int len)
{
    m_pbuf.dispose();
    m_pbuf = ALLOC_NEW BYTE[len + 1]();
    rc_error(m_pbuf.m_p != NULL, E_FAIL);
    m_msglen = len;
    return S_OK;
}
unsigned int CMsgImpl::GetLength()
{
    return m_msglen;
}
HRESULT CMsgImpl::SetMsgRawData(BYTE* data, unsigned int size, unsigned int offset)
{
    rc_error(((data != NULL) && size != 0), E_FAIL);
    s_memcpy(m_pbuf.m_p + offset, data, size);
    return S_OK;
}
BYTE* CMsgImpl::GetMsgRawData(unsigned int offset)
{
    rc_error(m_pbuf.m_p != NULL, NULL);
    return m_pbuf.m_p + offset;
}
HRESULT CMsgImpl::Clear()
{
    Reset();
    return S_OK;
}
HRESULT CMsgImpl::Attach(IMsg* pMsg)
{
    HRESULT hr = S_OK;

    hr = Clear();
    rc_error(hr == S_OK, E_FAIL);

    hr = SetMsgType(pMsg->GetMsgType());
    rc_error(hr == S_OK, E_FAIL);

    hr = SetId(pMsg->GetId());
    rc_error(hr == S_OK, E_FAIL);

    hr = SetVersion(pMsg->GetVersion());
    rc_error(hr == S_OK, E_FAIL);

    hr = SetLength(pMsg->GetLength());
    rc_error(hr == S_OK, E_FAIL);

    hr = SetMsgRawData(pMsg->GetMsgRawData(0), pMsg->GetLength(), 0);
    rc_error(hr == S_OK, E_FAIL);

    return S_OK;
}
HRESULT CMsgImpl::Reset()
{
    m_id = 0;
    m_version = 0;
    m_msgtype = 0;
    m_msglen = 0;
    m_pbuf.dispose();
    return S_OK;
}
